package com.lw.extraceim.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lw.extraceim.R;
import com.lw.extraceim.model.CustomerInfo;
import com.lw.extraceim.ui.activity.customer.CustomerEditActivity;

import java.util.List;

/**
 * @Author : liwei
 * @Email : 1759840027@qq.com
 * @Description : 客户列表的适配器
 * @Date : 2019/5/8 17:07
 */
public class CustomerAdapter extends BaseAdapter {
    private List<CustomerInfo> customerInfoList;
    private Activity mContext;
    private int visibility = View.GONE;

    public CustomerAdapter(List<CustomerInfo> customerInfoList, Activity mContext) {
        this.customerInfoList = customerInfoList;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        if (customerInfoList != null) {
            return customerInfoList.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return customerInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.customer_item, null);
            holder = new ViewHolder();
            holder.mTvCustomerName = convertView.findViewById(R.id.tv_cutomer_name);
            holder.mTvCustomerPhone = convertView.findViewById(R.id.tv_customer_phone);
            holder.mTvCustomerAddress = convertView.findViewById(R.id.tv_customer_address);
            holder.mOperationLayout = convertView.findViewById(R.id.operation_layout);
            holder.mButtonCustomerEdit = convertView.findViewById(R.id.button_customer_edit);
            holder.mButtonCustoemrDelete = convertView.findViewById(R.id.button_customer_delete);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final CustomerInfo customerInfo = customerInfoList.get(position);
        holder.mTvCustomerName.setText(customerInfo.getName());
        holder.mTvCustomerPhone.setText(customerInfo.getTelCode());
        holder.mTvCustomerAddress.setText(customerInfo.getRegionString() + " " + customerInfo.getAddress());
        holder.mOperationLayout.setVisibility(visibility);
        holder.mButtonCustoemrDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext,"你点击了删除按钮",Toast.LENGTH_SHORT).show();
            }
        });
        holder.mButtonCustomerEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CustomerEditActivity.actionStart(mContext,CustomerEditActivity.EDIT_ACTION,customerInfo);
            }
        });

        return convertView;

    }

    public int getVisibility() {
        return visibility;
    }

    public void setVisibility(int visibility) {
        this.visibility = visibility;
        notifyDataSetChanged();
    }

    public List<CustomerInfo> getCustomerInfoList() {
        return customerInfoList;
    }

    public void setCustomerInfoList(List<CustomerInfo> customerInfoList) {
        this.customerInfoList = customerInfoList;
        notifyDataSetChanged();
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    class ViewHolder {
        public TextView mTvCustomerName;
        public TextView mTvCustomerPhone;
        public TextView mTvCustomerAddress;

        public RelativeLayout mOperationLayout;
        public Button mButtonCustomerEdit;
        public Button mButtonCustoemrDelete;
    }
}
